import asyncio
import os
import sys
from app.core.config import settings
from app.core.langgraph.sample_agent import SampleAgent

# 在Windows系统上解决psycopg与asyncio的兼容性问题
if sys.platform == 'win32':
    from asyncio import WindowsSelectorEventLoopPolicy

    asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())

from app.core.langgraph.graph import LangGraphAgent

os.environ["ENVIRONMENT"] = "development"
print(f"环境: {settings.ENVIRONMENT}")
print(f"数据库URL: {settings.POSTGRES_URL}")


async def main():
    # 创建代理实例
    agent = SampleAgent()

    # 创建图（这是异步方法，需要 await）
    graph = await agent.create_graph()

    # 现在可以测试图的调用
    if graph is not None:
        # 注意：需要传入正确的 GraphState 格式和配置
        config = {
            "configurable": {
                "thread_id": "test_session_123"
            }
        }

        input_data = {
            "messages": [{"role": "user", "content": "查一下太仓的天气"}],
            "session_id": "test_session_123"
        }

        try:
            res = await graph.ainvoke(input_data, config)
            print("res=",res["messages"])
        except Exception as e:
            print(f"调用图时出错: {e}")
            import traceback
            traceback.print_exc()
    else:
        print("图创建失败")


if __name__ == "__main__":
    asyncio.run(main())
